package com.wltjack.controller;

import com.wltjack.common.Response;
import com.wltjack.dao.model.User;
import com.wltjack.dao.model.UserEntity;
import com.wltjack.service.User4JpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user/jpa")
public class User4JpaController {

    @Autowired
    private User4JpaService user4JpaService;

    /**
     * 查询所有的记录
     */
    @GetMapping(value = "/findAll")
    public Response findAll() {
        List<User> userList = user4JpaService.findAll();
        if (userList != null) {
            Response response = new Response(0,"成功",userList);
            return response;
        }
        return new Response(-1000,"not found data",null);
    }

    /**
     * 分页查询记录
     * http://127.0.0.1:8080/learn/user/jpa/getPage?pageNo=0&pageSize=5
     */
    @GetMapping("/getPage")
    public Response getPage(@RequestParam(value="pageNo", defaultValue = "0") Integer pageNo,
                            @RequestParam(value="pageSize", defaultValue = "10") Integer pageSize){
        Page<User> page = user4JpaService.getPage(pageNo, pageSize);
        if (page != null) {
            Response response = new Response(0,"成功", page);
            return response;
        }
        return new Response(-1000,"not found data", null);
    }

    /**
     * 分页模糊查询记录
     * http://127.0.0.1:8080/learn/user/jpa/searchUser?pageNo=0&pageSize=5
     */
    @GetMapping("/searchUser")
    public Response searchUser(@RequestParam(value="pageNo", defaultValue = "0") Integer pageNo,
                            @RequestParam(value="pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(value="searchText") String searcheText){
        Page<User> page = user4JpaService.searchUser(pageNo, pageSize, searcheText);
        if (page != null) {
            Response response = new Response(0,"成功",page);
            return response;
        }
        return new Response(-1000,"成功","not found data");
    }
}
